package components
{
	import UIFree.ObjectEvent;
	import UIFree.uif_component;
	
	import classes.c_events;
	
	import flash.events.MouseEvent;
	
	public class c_control extends c_component {
		private static var _curCursrorControl: c_control;
		private var _cursorName	: String;
		
		public function c_control(parent:uif_component, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, styleClass:Object='') {
			super(parent, ax, ay, awidth, aheight, styleClass);
		}
		
		public function get cursorName(): String {
			return _cursorName;
		}
		
		public function set cursorName(a_cursorName: String): void {
			_cursorName = a_cursorName;
			if (_cursorName) {
				addEvent(MouseEvent.ROLL_OVER, doRollOver);
				addEvent(MouseEvent.ROLL_OUT, doRollOut);
				addEvent(MouseEvent.MOUSE_UP, doMouseUp, stage);
			} else {
				removeEventListener(MouseEvent.ROLL_OVER, doRollOver);
				removeEventListener(MouseEvent.ROLL_OUT, doRollOut);
				removeEventListener(MouseEvent.MOUSE_UP, doMouseUp);
			}
		}
		
		protected function doMouseUp(e: MouseEvent): void {
			if ((_curCursrorControl == this) && (!_curCursrorControl.hitTestPoint(stage.mouseX, stage.mouseY))) {
				cl.workspace.dispatchEvent(new ObjectEvent(c_events.SETCURSOR, ''));
				_curCursrorControl = null;
			}
		}
		
		protected function doRollOver(e: MouseEvent): void {
			if (!e.buttonDown) { 
				cl.workspace.dispatchEvent(new ObjectEvent(c_events.SETCURSOR, cursorName));
				_curCursrorControl = this;
			}
		}

		protected function doRollOut(e: MouseEvent): void {
			if (!(e.relatedObject is c_cursor)) {
				if (!e.buttonDown && (_curCursrorControl == this)) {
					cl.workspace.dispatchEvent(new ObjectEvent(c_events.SETCURSOR, ''));
					_curCursrorControl = null;
				}
			}
		}
	}
}